package com.atguigu.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;

public class PublicUtils {
    private static final String STRING_NULL = "-";
    private static final String REGX_MOBILENUM = "^((\\+86)|(86))?(13|15|17|18)\\d{9}$";
    private static final String REGX_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    public static final String SPOT = ".";

    public static Boolean isMobileNumber(String inputStr) {
        return !isNull(inputStr) && inputStr.matches("^((\\+86)|(86))?(13|15|17|18)\\d{9}$");
    }

    public static boolean isEmpty(Object pObj) {
        if (pObj == null) {
            return true;
        } else if (pObj == "") {
            return true;
        } else if (pObj instanceof String) {
            return ((String)pObj).length() == 0;
        } else if (pObj instanceof Collection) {
            return ((Collection)pObj).isEmpty();
        } else if (pObj instanceof Map) {
            return ((Map)pObj).size() == 0;
        } else {
            return false;
        }
    }

    public static boolean isNotEmpty(Object pObj) {
        if (pObj == null) {
            return false;
        } else if (pObj == "") {
            return false;
        } else if (pObj instanceof String) {
            return ((String)pObj).length() != 0;
        } else if (pObj instanceof Collection) {
            return !((Collection)pObj).isEmpty();
        } else if (pObj instanceof Map) {
            return ((Map)pObj).size() != 0;
        } else {
            return true;
        }
    }

    public static boolean isNull(Object... values) {
        if (!isNotNullAndNotEmpty(values)) {
            return true;
        } else {
            Object[] var1 = values;
            int var2 = values.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Object value = var1[var3];
                boolean flag;
                if (value instanceof Object[]) {
                    flag = !isNotNullAndNotEmpty((Object[])((Object[])value));
                } else if (value instanceof Collection) {
                    flag = !isNotNullAndNotEmpty((Collection)value);
                } else if (value instanceof String) {
                    flag = isOEmptyOrNull(value);
                } else {
                    flag = null == value;
                }

                if (flag) {
                    return true;
                }
            }

            return false;
        }
    }

    private static boolean isOEmptyOrNull(Object o) {
        return o == null || isSEmptyOrNull(o.toString());
    }

    private static boolean isSEmptyOrNull(String s) {
        return trimAndNullAsEmpty(s).length() <= 0;
    }

    private static Boolean isNotNullAndNotEmpty(Collection<?> value) {
        boolean bl = false;
        if (null != value && !value.isEmpty()) {
            bl = true;
        }

        return bl;
    }

    private static Boolean isNotNullAndNotEmpty(Object[] value) {
        boolean bl = false;
        if (null != value && 0 < value.length) {
            bl = true;
        }

        return bl;
    }

    private static String trimAndNullAsEmpty(String s) {
        return s != null && !s.trim().equals("-") ? s.trim() : "";
    }

    public static boolean isEmail(String str) {
        boolean bl = true;
        if (isSEmptyOrNull(str) || !str.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) {
            bl = false;
        }

        return bl;
    }

    public static synchronized String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String maskMobileNo(String mobileNo) {
        if (!isEmpty(mobileNo)) {
            int len = mobileNo.length();
            if (len > 4) {
                return mobileNo.substring(0, len - 4) + "****";
            }
        }

        return mobileNo;
    }

    public static int returnCount(String sourceTarget) {
        int count = 0;

        for(int i = 0; i < sourceTarget.length(); ++i) {
            String s = String.valueOf(sourceTarget.charAt(i));
            if (s.equals("、")) {
                ++count;
            }
        }

        return count;
    }

    public static String parseNumber(String pattern, BigDecimal bd) {
        if (isEmpty(bd)) {
            return null;
        } else {
            DecimalFormat df = new DecimalFormat(pattern);
            String result = df.format(bd);
            if (BigDecimal.ONE.compareTo(bd) > 0 && BigDecimal.ZERO.compareTo(bd) <= 0) {
                if (result.startsWith(".")) {
                    result = "0" + result;
                }
            } else if (bd.compareTo(new BigDecimal(-1)) > 0 && BigDecimal.ZERO.compareTo(bd) > 0) {
                result = "-0" + result.replace("-", "");
            }

            return result;
        }
    }

    public static String replaceZeroAndDot(String str) {
        if (isEmpty(str)) {
            return null;
        } else {
            if (str.indexOf(".") > 0) {
                str = str.replaceAll("0+?$", "");
                str = str.replaceAll("[.]$", "");
            }

            return str;
        }
    }

    public static String returnName(String sourceTarget) {
        int index = sourceTarget.indexOf("、");
        index = sourceTarget.indexOf("、", index + 1);
        String result = sourceTarget.substring(0, index);
        return result;
    }

    public static String emptyFormat(String sourceString) {
        return isEmpty(sourceString) ? "-" : sourceString;
    }

    public static String addString(String source, String target) {
        if (isEmpty(source) && isEmpty(target)) {
            return "";
        } else if (isEmpty(source) && isNotEmpty(target)) {
            return target.startsWith(",") ? target.substring(1) : target;
        } else if (isNotEmpty(source) && isEmpty(target)) {
            return source.startsWith(",") ? source.substring(1) : source;
        } else if (source.contains(target)) {
            return source.startsWith(",") ? source.substring(1) : source;
        } else {
            return source.startsWith(",") ? source.substring(1) : source + "," + target;
        }
    }

    public static String setString(String source, String target) {
        if (isEmpty(source) && isEmpty(target)) {
            return "";
        } else if (isEmpty(source) && isNotEmpty(target)) {
            return "";
        } else if (isNotEmpty(source) && isEmpty(target)) {
            return source.startsWith(",") ? source.substring(1) : source;
        } else {
            source = source.replaceAll(target, "");
            return source.startsWith(",") ? source.substring(1) : source;
        }
    }

    public static String addBeforeString(String source, String target) {
        if (isEmpty(source) && isEmpty(target)) {
            return "";
        } else if (isEmpty(source) && isNotEmpty(target)) {
            return target.startsWith(",") ? target.substring(1) : target;
        } else if (isNotEmpty(source) && isEmpty(target)) {
            return source.startsWith(",") ? source.substring(1) : source;
        } else if (source.contains(target)) {
            return source.startsWith(",") ? source.substring(1) : source;
        } else {
            return target + "," + (source.startsWith(",") ? source.substring(1) : source);
        }
    }

    private PublicUtils() {
    }
}
